﻿using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.IO;
using System.Reflection;

using IronPython.Hosting;

namespace _GTD2003
{
    public partial class ThisAddIn
    {
        private PythonEngine _pythonEngine;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            InitializePythonEngine();
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        private void InitializePythonEngine()
        {
            _pythonEngine = new PythonEngine();
            _pythonEngine.Import("Site");

            FileStream fs = new FileStream(Path.Combine(PythonScriptPath,
                "python-output.txt"),
               FileMode.Create);
            _pythonEngine.SetStandardOutput(fs);
            _pythonEngine.SetStandardError(fs);
            // Starting up ...
            _pythonEngine.Globals["outlook"] = this.Application;
            _pythonEngine.Globals["pyScriptPath"] = PythonScriptPath;
            _pythonEngine.AddToPath(PythonScriptPath);
            _pythonEngine.AddToPath(Path.Combine(PythonScriptPath, "Lib"));
            _pythonEngine.ExecuteFile(Path.Combine(PythonScriptPath, "Startup.py"));
        }

        string PythonScriptPath
        {
            get
            {
                string uriPath = Assembly.GetAssembly(GetType()).CodeBase;
                return Path.Combine(uriPath.Substring("file:///".Length),
                    "../Python");
            }
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
